

local string = string
local nprint = io.write

string.trim = function (s) 
  return (string.gsub(s, "^%s*(.-)%s*$", "%1")) 
end

function Log(str, ...)
	local output = string.format(str, ...)
end

function p(value, deep)
	deep = deep or 0
	if deep > 3 then print(spc .. "...") return end

	local spc = ""

	for i=1, deep do spc = spc .. '\t' end

	local ty = type(value)
	if ty and ty ~= 'table' then
		print(tostring(value))
	else
		print(spc .. "{")
		for k, v in pairs(value) do
			nprint(spc .. '\t['..k ..']'.." = ")
			p(v, deep + 1)
		end
		print(spc .. "}")
	end
end


-- convert table to str
function tabToStr( tab, is )
	if tab == nil then
		return nil;
	end;
	local result = "{"

	for k,v in pairs(tab) do
		if type(k) == "number" then
			if type(v) == "table" then
				result = string.format( "%s[%d]=%s,", result, k, __tabToStr( v ,is) )
			elseif type(v) == "number" then
				result = string.format( "%s[%d]=%d,", result, k, v )
			elseif type(v) == "string" then
				result = string.format( "%s[%d]=%q,", result, k, v )
			elseif type(v) == "boolean" then
				result = string.format( "%s[%d]=%s,", result, k, tostring(v) )
			else
				if is then
					result = string.format("%s[%d]=%q,", result, k, type(v) )
				else
					error("the type of value is a function or userdata")
				end
			end
		else
			if type(v) == "table" then
				result = string.format( "%s%s=%s,", result, k, __tabToStr( v, is ) )
			elseif type(v) == "number" then
				result = string.format( "%s%s=%d,", result, k, v )
			elseif type(v) == "string" then
				result = string.format( "%s%s=%q,", result, k, v )
			elseif type(v) == "boolean" then
				result = string.format( "%s%s=%s,", result, k, tostring(v) )
			else
				if is then
					result = string.format( "%s[%s]=%q,", result, k, type(v) )
				else
					error("the type of value is a function or userdata")
				end
			end
		end
	end
	result = result .. "}"
	return result
end

